package handler

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"path"
	"path/filepath"
	"time"
	"zy/api/request"
	"zy/internal/dao"
	"zy/internal/model"
	"zy/pkg"
)

func AddExam(c *gin.Context) {
	var req request.AddExamReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	var e model.Exam
	dao.GetOneByFields(&model.Exam{

		ExamTitle: req.ExamTitle,
	}, &e)
	if e.Id != 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "试题已存在",
			"data": nil,
		})
		return
	}
	file, err := c.FormFile("file")
	if err != nil {
		return
	}

	filename := fmt.Sprintf("%d%s", time.Now().UnixMilli(), path.Ext(file.Filename))
	if file.Size > 5*1024*1024*1024 {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "文件过大",
			"data": false,
		})
		return
	}
	ext := filepath.Ext(filename)
	if ext != ".pdf" && ext != ".word" {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "文件格式错误",
			"data": false,
		})
		return
	}
	dst := "D:\\GOWORK\\zk1\\zy\\static/" + filename
	err = c.SaveUploadedFile(file, dst)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "文件上传失败",
			"data": false,
		})
		return
	}
	e = model.Exam{

		ExamTitle:   req.ExamTitle,
		ExamType:    req.ExamType,
		Keywords:    req.Keywords,
		Difficulty:  req.Difficulty,
		MajorOne:    req.MajorOne,
		MajorTwo:    req.MajorTwo,
		Author:      req.Author,
		Name:        req.Name,
		CTimes:      req.CTimes,
		Reviewer:    req.Reviewer,
		AuditStatus: req.AuditStatus,
		ScopeUse:    req.ScopeUse,
	}
	if !dao.Create(&e) {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "试题发布失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "试题发布成功",
		"data": e,
	})

}

func Uploads(c *gin.Context) {
	file, err := c.FormFile("file")
	if err != nil {
		return
	}

	filename := fmt.Sprintf("%d%s", time.Now().UnixMilli(), path.Ext(file.Filename))
	if file.Size > 5*1024*1024*1024 {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "文件过大",
			"data": false,
		})
		return
	}

	ext := filepath.Ext(filename)

	if ext != ".png" && ext != ".jpg" {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "图片格式错误",
			"data": false,
		})
		return
	}
	dst := "D:\\GOWORK\\zk1\\zy\\static/" + filename
	err = c.SaveUploadedFile(file, dst)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "图片上传失败",
			"data": false,
		})
		return
	}
	pkg.OssFile(dst, filename)
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "图片上传成功",
		"data": true,
	})
}

func ListExam(c *gin.Context) {
	var req request.ListExamReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	fmt.Println(req.ExamType)
	exam := dao.GetExam(req.ExamType)

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "展示成功",
		"data": exam,
	})

	//Id:          exam.Id,
	//	ExamTitle:   exam.ExamTitle,
	//		ExamType:    exam.ExamType,
	//		Keywords:    exam.Keywords,
	//		Difficulty:  exam.Difficulty,
	//		MajorOne:    exam.MajorOne,
	//		MajorTwo:    exam.MajorTwo,
	//		Author:      exam.Author,
	//		Name:        exam.Name,
	//		CTimes:      exam.CTimes,
	//		Reviewer:    exam.Reviewer,
	//		AuditStatus: exam.AuditStatus,
	//		ScopeUse:    exam.ScopeUse,
}
